﻿using System;
using System.Web.UI;
using Trirand.Web.UI.WebControls;
using Web.Account;

namespace AssetManage.Manage
{
    public partial class Detail : Page
    {
        private static String _expenseInquirySql;
        private static String _tableidSql;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && Expense.AjaxCallBackMode == AjaxCallBackMode.None)
            {
                Master.OnClickLink("detail");

                StartDate.Text = DateTime.Today.AddDays(-60).Date.ToString("yyyy/MM/dd");
                EndDate.Text = DateTime.Today.Date.ToString("yyyy/MM/dd");

                if (Context.User.IsInRole("Administrator"))
                {
                    _tableidSql = "SELECT [TableID], [RealName] FROM [Account] WHERE ([TableID] <> 'Administrator')";
                }
                else if (Context.User.IsInRole("User"))
                {
                    _tableidSql =
                        "SELECT [TableID], [RealName] FROM [Account] WHERE (([TableID] <> 'Root') AND (([TableID] = 'Public') OR ([TableID] = '" +
                        AccountManage.GetTableID(Page.User.Identity.Name) + "')))";
                }
                else
                {
                    _tableidSql = "SELECT [TableID], [RealName] FROM [Account] WHERE ([TableID] = 'Public')";
                }
                Account_SqlDataSource.SelectCommand = _tableidSql;
                UserID.DataBind();
            }
            Expense_SqlDataSource.SelectCommand = _expenseInquirySql;
        }

        protected void UserID_DataBound(object sender, EventArgs e)
        {
            UserID.SelectedValue = "Public";
        }

        protected void InquiryButton_Click(object sender, EventArgs e)
        {
            _expenseInquirySql = "SELECT [Time], [Expense], [Item] FROM [" + UserID.SelectedValue +
                                  "] WHERE (([Time] >= #" + StartDate.Text + " 00:00:00#) AND ([Time] <= #" +
                                  EndDate.Text + " 23:59:59#)) ORDER BY [Time] DESC, [ID] DESC;";
            Expense_SqlDataSource.SelectCommand = _expenseInquirySql;
            ClientScript.RegisterStartupScript(Page.GetType(), null, "ShowDialog();", true);
        }

        protected void ExportToExcel_Click(object sender, EventArgs e)
        {
            var startTime = DateTime.ParseExact(StartDate.Text, "yyyy/MM/dd", null);
            var endTime = DateTime.ParseExact(EndDate.Text, "yyyy/MM/dd", null);
            var excelStream = AccountManage.ExportToExcel(UserID.SelectedValue, startTime, endTime);
            if (excelStream == null) return;
            Response.AddHeader("Content-Disposition",
                "attachment; filename=Public_from" + StartDate.Text.Replace(@"/", "") + "_to" +
                EndDate.Text.Replace(@"/", "") + ".xls");
            Response.BinaryWrite(excelStream.ToArray());
            excelStream.Close();
            excelStream.Dispose();
        }
    }
}